package org.david.code.web.action;

import java.io.IOException;
import java.util.Locale;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class BaseAction extends ActionSupport {

    private String errorMessage = "";
    protected Map getParameters() {
        return ActionContext.getContext().getParameters();
    }
    protected Object getParameter(Object key) {
        Map m = getParameters();
        Object[] obj=(Object[])m.get(key);
        return obj==null?null:obj[0];
    }

    public HttpServletRequest getRequest() {
        return ServletActionContext.getRequest();
    }

    public HttpServletResponse getResponse() {
        return ServletActionContext.getResponse();
    }

    public String getErrorMessage() {
        return errorMessage;
    }

    public void setErrorMessage(String errorMessage) {
        this.errorMessage = errorMessage;
    }
    protected Map getRequestAttribute() {
        return (Map)ActionContext.getContext().get("request");
    }
    protected String getURI() {
        return ServletActionContext.getRequest().getRequestURI();
    }
    protected String getURL() {
        return ServletActionContext.getRequest().getRequestURL().toString();
    }
    protected String getQueryString() {
        String qs=ServletActionContext.getRequest().getQueryString();
        if(qs==null){
            qs=String.valueOf(ServletActionContext.getRequest().getAttribute("javax.servlet.forward.query_string"));
        }
        return qs;
    }
    protected String getContextPath() {
        return ServletActionContext.getRequest().getContextPath();
    }
    protected void setRequestAttribute(String name,Object obj) {
         ServletActionContext.getRequest().setAttribute(name, obj);
    }
    
    protected Map getSession() {
         return ActionContext.getContext().getSession();
    }
    
    protected void sendRedirect(String path){
        try {
            ServletActionContext.getResponse().sendRedirect(path);
        } catch (IOException e) {
            e.printStackTrace();
        }
        
    }
    
    protected void setError(String anErrorMessage){
        addActionError(anErrorMessage);
    }
    protected void setMessage(String aMessage){
        addActionMessage(aMessage);
    }

    public String getLanguage(){
        return ServletActionContext.getRequest().getHeader("Accept-Language");
    }
    
    public Locale getLocal(){
        return ServletActionContext.getRequest().getLocale();
    }
    //获得项目绝对路劲
    public String getRootPath(){
		String rootpath= ServletActionContext.getServletContext().getRealPath("/");
		return dealPath(rootpath);
	}
	public static String dealPath(String filepath){
		if(filepath!=null&&filepath.length()>1){
			if(filepath.lastIndexOf("/")==filepath.length()-1){
				String str=filepath.substring(0,filepath.length()-1);
				return str.replaceAll("\\\\", "/");
			}
			return filepath;
		}
		return "";
		
	}
}
